#!/bin/sh
#
# PROVIDE: dhcpd
# REQUIRE: DAEMON
# BEFORE: LOGIN
# KEYWORD: shutdown

. /etc/rc.subr

name="dhcpd"
rcvar="dhcpd_enable"
start_precmd="dhcpd_precmd"
stop_postcmd="dhcpd_postcmd"
command="/usr/local/sbin/${name}"
pidfile="/var/run/${name}.pid"
required_files="/etc/${name}.conf"

dhcpd_precmd() {

        shadow_path=/shadow/var/db
        local_path=/var/db
        filename=${name}.leases

        echo "Restoring dhcpd leases from CF ..."
        cp -fp ${shadow_path}/${filename} ${local_path}/${filename}

        if [ $? -gt 0 ]; then
                echo "Failed to copy on-disk copy (dhcpd.leases) into MFS RD, creating blank ${local_path}/${filename}."
		/usr/bin/touch ${local_path}/${filename}
        fi

}

dhcpd_postcmd() {

        shadow_path=/shadow/var/db
        local_path=/var/db
        filename=${name}.leases

        echo "Archiving dhcpd leases to CF ..."
        cp -fp ${local_path}/${filename} ${shadow_path}/${filename}

        if [ $? -gt 0 ]; then
                echo "Failed to stow in-MD/FS copy (dhcpd.leases) to disk."
        fi

}

load_rc_config $name
run_rc_command "$1"
